/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.filter.ElementFilter;
import org.jdom.input.SAXBuilder;

/**
 *
 * @author thedaego
 */
public class ResourceConfiguration
{
    private File configurationFile;
    private String defaultConfigurationFile = "webresources.xml";
    private Document configDocument;
    
    public ResourceConfiguration() throws ScraperException{
        this(null);
    }
    public ResourceConfiguration(File file) throws ScraperException{
        if(file != null){
            this.configurationFile = file;
        }else{
            this.configurationFile =
                    new File(defaultConfigurationFile);
            
        }
        SAXBuilder saxbuilder = new SAXBuilder();
        try {
            this.configDocument = saxbuilder.build(configurationFile);
        } catch (JDOMException ex) {
            throw new ScraperException("Error creating SAXBuilder"
                    + "with config file: "
                    + configurationFile
                    + "\n"+ex.getMessage(), ex);
        } catch (IOException ex) {
            throw new ScraperException("Error creating SAXBuilder"
                    + "with config file: "
                    + configurationFile
                    + "\n"+ex.getMessage(), ex);
        }
    }
    public List<WebResource> getAllWebResources()
    {
        List<WebResource> webResources = new ArrayList<WebResource>();
        Element rootE = configDocument.getRootElement();
        for (Iterator<Element> it = rootE.getChildren().iterator(); it.hasNext();)
        {
            Element WebResourceE = it.next();
            WebResource wr = new WebResource(WebResourceE.getChild("resourceName").getText());
            webResources.add(wr);            
        }
        for (WebResource webResource : webResources)
        {
            webResource.setDataLocators(getAllDataLocators(webResource.getName()));
        }
        return webResources;
    }

    public List<DataLocator> getAllDataLocators(String resourceName)
    {
        List<DataLocator> dataLocators = new ArrayList<DataLocator>();
        Element webResourceE = getResourceElement(resourceName);
        Element dataLocatorsE = getElement(webResourceE, "dataLocators");

        for (Iterator<Element> it = dataLocatorsE.getChildren().iterator(); it.hasNext();)
        {
            DataLocator locator = new DataLocator();
            Element dataLocatorE = it.next();
            locator.setLocatorName(dataLocatorE.getChild("locatorName").getText());
            locator.setLocatorUrl(dataLocatorE.getChild("locatorUrl").getText());
            locator.setXpath(dataLocatorE.getChild("xpath").getText());

            dataLocators.add(locator);

        }
        return dataLocators;

    }
    private Element getResourceElement(String resourceName)
    {
        Element e = null;
        Element root = configDocument.getRootElement();
        List webresources = root.getChildren("WebResource");
        for (Object object : webresources)
        {
            Element webresource = (Element)object;
            List resourceProps = webresource.getChildren();
            for (Object object1 : resourceProps)
            {
                Element resourceProp = (Element)object1;
                if(resourceProp.getName().equals("resourceName")){
                    if(resourceProp.getText().equals(resourceName)){
                        e = webresource;
                    }
                }
            }
        }
        return e;
        
    }
    private Element getElement(Element parentElement, String elementName)
    {
        Element returnElement=null;
        ElementFilter ef = new ElementFilter();
        for (Iterator<Element> it = parentElement.getDescendants(ef); it.hasNext();)
        {
            Element e = it.next();
            if(e.getName().equals(elementName)){
                returnElement= e;
                break;
            }
            if(!it.hasNext()){
                getElement(e, elementName);
            }
        }

        return returnElement;
    }
}
